package com.ysd.lis.controller.eq;


import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ysd.common.Result;
import com.ysd.common.dto.ColumnValueDto;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.eq.EqPatrolTask;
import com.ysd.lis.entity.eq.EqPatrolTaskEq;
import com.ysd.lis.entity.eq.EqPatrolTaskEqSt;
import com.ysd.lis.entity.eq.EqPatrolTaskEqStIt;
import com.ysd.lis.service.eq.EqPatrolTaskEqService;
import com.ysd.lis.service.eq.EqPatrolTaskEqStItService;
import com.ysd.lis.service.eq.EqPatrolTaskEqStService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 巡检记录设备的巡检标准细项 前端控制器
 * </p>
 *
 * @author WH
 * @since 2024-08-15
 */
@RestController
@RequestMapping("/api/eq/patrolTaskEqStIt")
public class EqPatrolTaskEqStItController extends BaseController {

    @Autowired
    private EqPatrolTaskEqService eqPatrolTaskEqService;
    @Autowired
    private EqPatrolTaskEqStService eqPatrolTaskEqStService;
    @Autowired
    private EqPatrolTaskEqStItService eqPatrolTaskEqStItService;
    @ApiOperation("查询巡检记录设备的巡检标准细项")
    @GetMapping("/findPatrolTaskEqStItList")
    public Result findPatrolTaskEqStItList(EqPatrolTaskEqStIt eqPatrolTaskEqStIt) {
        return eqPatrolTaskEqStItService.findPatrolTaskEqStItList(eqPatrolTaskEqStIt);
    }

    @ApiOperation("批量修改或新增")
    @PostMapping("/saveOrUpdateTaskEqStIt")
    public Result saveOrUpdateTaskEqStIt(@RequestBody List<EqPatrolTaskEqStIt> eqPatrolTaskEqStIts) {
        //同时修改结果/不通过/未检部分
        EqPatrolTaskEq eqPatrolTaskEq = eqPatrolTaskEqService.getById(eqPatrolTaskEqStIts.get(0).getTaskEqId());
        List<String> taskEqStIds = eqPatrolTaskEqStIts.stream().map(EqPatrolTaskEqStIt::getTaskEqStId).distinct().collect(Collectors.toList());

        //获取所有的巡检标准
        List<EqPatrolTaskEqSt> eqPatrolTaskEqSts = eqPatrolTaskEqStService.list(new LambdaQueryWrapper<EqPatrolTaskEqSt>().in(EqPatrolTaskEqSt::getId, taskEqStIds).eq(EqPatrolTaskEqSt::getDelFlag, 0));
        String isNormal="";
        List<String> noPass=new ArrayList<>();
        List<String> noCheck=new ArrayList<>();
        List<String> isNormales = eqPatrolTaskEqStIts.stream().filter(item-> StringUtils.isNotBlank(item.getIsNormal())).map(EqPatrolTaskEqStIt::getIsNormal).distinct().collect(Collectors.toList());

        if (CollectionUtils.isNotEmpty(isNormales)){
            isNormal="正常";
            for (String item : isNormales) {
                if ("异常".equals(item)){
                    isNormal=item;
                    break;
                }
            }
        }
        for (EqPatrolTaskEqStIt eqPatrolTaskEqStIt : eqPatrolTaskEqStIts) {
            if ("异常".equals(eqPatrolTaskEqStIt.getIsNormal())){
                EqPatrolTaskEqSt eqPatrolTaskEqSt = eqPatrolTaskEqSts.stream().filter(item -> item.getId().equals(eqPatrolTaskEqStIt.getTaskEqStId())).findFirst().get();
                noPass.add(eqPatrolTaskEqSt.getStandardName());
            }
        }
        for (EqPatrolTaskEqStIt eqPatrolTaskEqStIt : eqPatrolTaskEqStIts) {
            if (StringUtils.isBlank(eqPatrolTaskEqStIt.getCheckResult())){
                EqPatrolTaskEqSt eqPatrolTaskEqSt = eqPatrolTaskEqSts.stream().filter(item -> item.getId().equals(eqPatrolTaskEqStIt.getTaskEqStId())).findFirst().get();
                noCheck.add(eqPatrolTaskEqSt.getStandardName());
            }
        }
        List<String> noPassEs = noPass.stream().distinct().collect(Collectors.toList());
        eqPatrolTaskEq.setCheckResult(isNormal);
        eqPatrolTaskEq.setNoPass(String.join(",", noPassEs));
        eqPatrolTaskEq.setNoCheck(String.join(",", noCheck));
        eqPatrolTaskEqService.updateById(eqPatrolTaskEq);
        eqPatrolTaskEqStItService.saveOrUpdateBatch(eqPatrolTaskEqStIts);
        return Result.succ(1, "保存成功！", null);
    }
}
